package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.danmo.domain.DanmoInformationFlagship;
import com.muyu.danmo.domain.DanmoShoppingFlagship;
import com.muyu.danmo.mapper.DanmoInformationFlagshipMapper;
import com.muyu.danmo.service.IDanmoInformationFlagshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 个人信息名家推荐店铺Service业务层处理
 * 
 * @author yangle
 * @date 2024-12-17
 */
@Service
public class DanmoInformationFlagshipServiceImpl implements IDanmoInformationFlagshipService
{
    @Autowired
    private DanmoInformationFlagshipMapper danmoInformationFlagshipMapper;

    /**
     * 查询个人信息名家推荐店铺
     * 
     * @param id 个人信息名家推荐店铺主键
     * @return 个人信息名家推荐店铺
     */
    @Override
    public DanmoInformationFlagship selectDanmoInformationFlagshipById(Long id)
    {
        return danmoInformationFlagshipMapper.selectDanmoInformationFlagshipById(id);
    }

    /**
     * 查询个人信息名家推荐店铺列表
     * 
     * @param danmoInformationFlagship 个人信息名家推荐店铺
     * @return 个人信息名家推荐店铺
     */
    @Override
    public List<DanmoInformationFlagship> selectDanmoInformationFlagshipList(DanmoInformationFlagship danmoInformationFlagship)
    {
        return danmoInformationFlagshipMapper.selectDanmoInformationFlagshipList(danmoInformationFlagship);
    }

    /**
     * 新增个人信息名家推荐店铺
     *
     * @param danmoInformationFlagship 个人信息名家推荐店铺
     * @return 结果
     */
    @Transactional
    @Override
    public Result<Integer> insertDanmoInformationFlagship(DanmoInformationFlagship danmoInformationFlagship)
    {
        try {
            danmoInformationFlagship.setCreateTime(DateUtils.getNowDate());
            DanmoInformationFlagship danmoInformationFlagship2 = new DanmoInformationFlagship();
            Long userId = danmoInformationFlagship.getUserId();
            danmoInformationFlagship2.setUserId(userId);
            danmoInformationFlagship2.setInformationId(danmoInformationFlagship.getInformationId());
            danmoInformationFlagship2.setShoppingFlagshipId(danmoInformationFlagship.getShoppingFlagshipId());
            DanmoInformationFlagship danmoInformationFlagships = danmoInformationFlagshipMapper.selectDanmoInformationFlagships(danmoInformationFlagship2);
           if (danmoInformationFlagships!=null){
               return Result.warn("用户已添加过该店铺");
           }

            int rows = danmoInformationFlagshipMapper.insertDanmoInformationFlagship(danmoInformationFlagship);
            return Result.success(rows, "添加成功");
        }catch (Exception e){
            return Result.error("添加失败");
        }

    }

    /**
     * 修改个人信息名家推荐店铺
     *
     * @param danmoInformationFlagship 个人信息名家推荐店铺
     * @return 结果
     */
    @Transactional
    @Override
    public Result updateDanmoInformationFlagship(DanmoInformationFlagship danmoInformationFlagship)
    {
        try {
            danmoInformationFlagship.setUpdateTime(DateUtils.getNowDate());

            int i = danmoInformationFlagshipMapper.updateDanmoInformationFlagship(danmoInformationFlagship);
            return  Result.success(i,"修改成功");
        }catch (Exception e){
            return Result.error("修改失败");
        }

    }

    /**
     * 批量删除个人信息名家推荐店铺
     *
     * @param ids 需要删除的个人信息名家推荐店铺主键
     * @return 结果
     */
    @Transactional
    @Override
    public Result deleteDanmoInformationFlagshipByIds(Long[] ids)
    {
        try {

            int i = danmoInformationFlagshipMapper.deleteDanmoInformationFlagshipByIds(ids);
            return Result.success(i,"删除成功");
        }catch (Exception e){
            return Result.error("删除失败");
        }
    }


    /**
     * 删除个人信息名家推荐店铺信息
     *
     * @param id 个人信息名家推荐店铺主键
     * @return 结果
     */
    @Transactional
    @Override
    public Result deleteDanmoInformationFlagshipById(Long id)
    {
        try {

            int i = danmoInformationFlagshipMapper.deleteDanmoInformationFlagshipById(id);
            return Result.success(i,"删除成功");
        }catch (Exception e){
            return Result.error("删除失败");
        }

    }

    /**
     * 新增旗舰店信息
     * 
     * @param danmoInformationFlagship 个人信息名家推荐店铺对象
     */
    public void insertDanmoShoppingFlagship(DanmoInformationFlagship danmoInformationFlagship)
    {
        List<DanmoShoppingFlagship> danmoShoppingFlagshipList = danmoInformationFlagship.getDanmoShoppingFlagshipList();
        Long id = danmoInformationFlagship.getId();
        if (StringUtils.isNotNull(danmoShoppingFlagshipList))
        {
            List<DanmoShoppingFlagship> list = new ArrayList<DanmoShoppingFlagship>();
            for (DanmoShoppingFlagship danmoShoppingFlagship : danmoShoppingFlagshipList)
            {
                danmoShoppingFlagship.setId(id);
                list.add(danmoShoppingFlagship);
            }
            if (list.size() > 0)
            {
                danmoInformationFlagshipMapper.batchDanmoShoppingFlagship(list);
            }
        }
    }
}
